package com.example.qxapp.activity;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.qxapp.Bean.Post;
import com.example.qxapp.Bean.User;
import com.example.qxapp.MainActivity;
import com.example.qxapp.R;

import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.BmobUser;
import cn.bmob.v3.datatype.BmobRelation;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.http.I;
import cn.bmob.v3.listener.QueryListener;
import cn.bmob.v3.listener.UpdateListener;
import de.hdodenhof.circleimageview.CircleImageView;

public class ReciveActivity extends AppCompatActivity {
    private TextView username,contnet,time;
    private ImageView back,rec_collect;
    private CircleImageView touser;
    private String user_onlyid;
    private Button focus_or_not;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_recive);
        init();
        initdata();
        getisrelated();
        touser.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent it = new Intent(ReciveActivity.this,MyinfoActivity.class);
                it.putExtra("user_onlyid",user_onlyid);
                startActivity(it);
            }
        });



        username.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent it = new Intent(ReciveActivity.this,MyinfoActivity.class);
                it.putExtra("user_onlyid",user_onlyid);
                startActivity(it);
            }
        });
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        focus_or_not.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //关注监听
                //获取本app登录用户
                final User current_user = BmobUser.getCurrentUser(User.class);
                //新建一个User类，其实是现在对应页面的用户（点进来的）
                User this_user = new User();
                //设置objectid
                this_user.setObjectId(BmobUser.getCurrentUser(User.class).getObjectId());
                BmobRelation relation = new BmobRelation();

                focus_or_not.setText("已关注");
                focus_or_not.setTag("1");
                // 将这个用户列入关注列表里
                // 添加到多对多关联中
                relation.add(this_user);
                current_user.setFocuId(relation);
                current_user.increment("focusId_sum");
                current_user.update(new UpdateListener() {
                    @Override
                    public void done(BmobException e) {
                        if (e==null){
                            Toast.makeText(ReciveActivity.this, "关注成功", Toast.LENGTH_SHORT).show();
                        }else {
                            Toast.makeText(ReciveActivity.this, "关注失败"+e, Toast.LENGTH_SHORT).show();
                        }
                    }
                });


            }
        });

        rec_collect.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent in = getIntent();
                String Id = in.getStringExtra("id");
                BmobQuery<Post> bmobQuery = new BmobQuery<>();
                bmobQuery.getObject(Id, new QueryListener<Post>() {
                    @Override
                    public void done(Post post, BmobException e) {
                        if (post.getIsrelated().equals("0")){

                            Intent in = getIntent();
                            String Id = in.getStringExtra("id");
                            User user = BmobUser.getCurrentUser(User.class);
                            Post po = new Post();
                            po.setObjectId(Id);
                            po.setIsrelated("1");
                            BmobRelation relation = new BmobRelation();
                            relation.add(user);
                            po.setRelation(relation);
                            po.update(new UpdateListener() {
                                @Override
                                public void done(BmobException e) {
                                    if (e==null){
                                        rec_collect.setImageResource(R.drawable.shoucang_black);
                                        Toast.makeText(ReciveActivity.this, "收藏成功", Toast.LENGTH_SHORT).show();
                                    }else {
                                        Toast.makeText(ReciveActivity.this, "收藏失败", Toast.LENGTH_SHORT).show();
                                    }
                                }
                            });
                        }else {
                            Intent in = getIntent();
                            String Id = in.getStringExtra("id");
                            User user = BmobUser.getCurrentUser(User.class);
                            Post po = new Post();
                            po.setObjectId(Id);
                            po.setIsrelated("0");
                            BmobRelation relation = new BmobRelation();
                            relation.remove(user);
                            po.setRelation(relation);
                            po.update(new UpdateListener() {
                                @Override
                                public void done(BmobException e) {
                                    if (e==null){
                                        rec_collect.setImageResource(R.drawable.sc_normal);
                                        Toast.makeText(ReciveActivity.this, "已取消收藏", Toast.LENGTH_SHORT).show();
                                    }else {
                                        Toast.makeText(ReciveActivity.this, "取消收藏失败", Toast.LENGTH_SHORT).show();
                                    }
                                }
                            });
                        }
                    }
                });

            }
        });
    }

    private void getisrelated() {
        Intent in = getIntent();
        String Id = in.getStringExtra("id");
        BmobQuery<Post> bmobQuery = new BmobQuery<>();
        bmobQuery.getObject(Id, new QueryListener<Post>() {
            @Override
            public void done(Post post, BmobException e) {
                if (post.getIsrelated().equals("1")){
                    //已被收藏
                    rec_collect.setImageResource(R.drawable.shoucang_black);
                }else {
                    //无收藏
                    rec_collect.setImageResource(R.drawable.sc_normal);
                }
            }
        });
    }

    private void initdata() {
        //第二种 加载快
        Intent intent=getIntent();
        String username1=intent.getStringExtra("username");
        String content1=intent.getStringExtra("content");
        String time1=intent.getStringExtra("time");
        username.setText(username1);
        contnet.setText(content1);
        time.setText(time1);



        //第一种联网
//        Intent intent=getIntent();
//        String id=intent.getStringExtra("id");
//        Post post=new Post();
//        BmobQuery<Post> query=new BmobQuery<>();
//        query.getObject(id, new QueryListener<Post>() {
//            @Override
//            public void done(Post post, BmobException e) {
//           if (e==null){
//               username.setText(post.getName());
//               contnet.setText(post.getContent());
//               time.setText(post.getCreatedAt());
//           }else {
//               Toast.makeText(ReciveActivity.this,"获取失败",Toast.LENGTH_SHORT).show();
//           }
//            }
//        });
//        back.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                finish();
//            }
//        });
    }

    private void init() {
        touser=findViewById(R.id.touser);
        username=findViewById(R.id.username);
        contnet=findViewById(R.id.content);
        time=findViewById(R.id.time);
        back=findViewById(R.id.back);
        rec_collect=findViewById(R.id.rec_collect);
        focus_or_not=findViewById(R.id.focus_or_not);
    }
}
